<?php
if ( ! defined( 'IN_IPB' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
	exit();
}

class task_item
{
	var $class     = "";
	var $root_path = "";
	var $task      = "";
	
	function run_task()
	{
		$this->task['task_log'] = 1;
		$this->class->append_task_log( $this->task, 'New registrations recieved new money and post rewards are recieved' );
		
		// Get the time of the last log
		$last_log = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'task_logs','where'=>"log_title='".$this->task['task_title']."'",'order'=>'log_date DESC','limit'=>array(1,1)));
		$last_log['log_date'] = intval($last_log['log_date']);
		
		$this->ipsclass->numbers = $this->ipsclass->load_class(ROOT_PATH."sources/market/numbers.php",'numbers');
		$this->ipsclass->numbers->load_checks();
		
		$members = array();
		$registered = array();
		$posted = array();
		$replies = array();
		$topics = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'members','where'=>"joined > ".$last_log['log_date']));
		$mems = $this->ipsclass->DB->exec_query();
		while($member = $this->ipsclass->DB->fetch_row($mems))
		{
			$members[$member['id']] = $this->ipsclass->vars['market_register'];
			$registered[$member['id']] = $this->ipsclass->vars['market_register'];
		}
		
		if(! is_array( $this->ipsclass->forums->forum_by_id ) OR !count( $this->ipsclass->forums->forum_by_id )) // Running Initiate
		{
			$this->ipsclass->forums->forums_init();
		}
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'p.author_id,p.new_topic',
												'from' => array('posts' => 'p'),
												'add_join' => array(
																		array(
																				'select' => 't.forum_id',
																				'from' => array('topics' => 't'),
																				'where' => "p.topic_id = t.tid",
																				'type' => 'left' 
																			),
																	),
												'where' => "p.post_date > ".$last_log['log_date'],
												'order' => 'post_date ASC',
										)	);
		$posts = $this->ipsclass->DB->exec_query();
		while($post = $this->ipsclass->DB->fetch_row($posts))
		{
			if($post['new_topic'])
			{
				if($this->ipsclass->forums->forum_by_id[$post['forum_id']]['market_topic'])
				{
					$more_more_money = $this->ipsclass->forums->forum_by_id[$post['forum_id']]['market_topic_amount'];
				}
				else
				{
					$more_more_money = $this->ipsclass->vars['market_post_topic'];
				}
			}
			else
			{
				if($this->ipsclass->forums->forum_by_id[$post['forum_id']]['market_reply'])
				{
					$more_more_money = $this->ipsclass->forums->forum_by_id[$post['forum_id']]['market_reply_amount'];
				}
				else
				{
					$more_more_money = $this->ipsclass->vars['market_post_reply'];
				}
				$replies[$post['topic_id']]++;
			}
			$members[$post['author_id']] = $this->ipsclass->numbers->add($members[$post['author_id']],$more_more_money);
			$posted[$post['author_id']] = $this->ipsclass->numbers->add($posted[$post['author_id']],$more_more_money);
			$topics[$post['topic_id']] = $post['topic_id'];
		}
		
		if($this->ipsclass->vars['market_topic_reply'] && count($replies))
		{
			if(count($topics))
			{
				$this->ipsclass->DB->build_query(array(
														'select' => 'COUNT(p.pid) as posts,p.topic_id',
														'from' => array('posts' => 'p'),
														'add_join' => array(
																				array(
																						'select' => 't.starter_id',
																						'from' => array('topics' => 't'),
																						'where' => "p.topic_id = t.tid",
																						'type' => 'left' 
																					),
																			),
														'where' => "p.post_date <= ".$last_log['log_date']." AND p.topic_id IN (".implode(', ',$topics).")",
														'group' => 'p.topic_id',
														'order' => 'post_date ASC',
												)	);
				$posts = $this->ipsclass->DB->exec_query();
				while($post = $this->ipsclass->DB->fetch_row($posts))
				{
					if($post['posts'] > $this->ipsclass->vars['market_topic_reply'] + 1)
					{
						continue;
					}
					
					if($post['posts'] + $replies[$post['topic_id']] > $this->ipsclass->vars['market_topic_reply'] + 1)
					{
						$members[$post['starter_id']] = $this->ipsclass->numbers->add($members[$post['starter_id']],$this->ipsclass->vars['market_topic_amount']);
						$posted[$post['starter_id']] = $this->ipsclass->numbers->add($posted[$post['starter_id']],$this->ipsclass->vars['market_topic_amount']);
					}
				}
			}
		}
		
		if(count($members) > 0)
		{
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'members','where'=>"id IN (".implode(",",array_keys($members)).")"));
			$mems = $this->ipsclass->DB->exec_query();
			while($mem = $this->ipsclass->DB->fetch_row($mems))
			{
				$this->ipsclass->DB->do_update('members',array(
																	$this->ipsclass->vars['market_members_field'] => $this->ipsclass->numbers->add($mem[$this->ipsclass->vars['market_members_field']],$members[$mem['id']])
											),"id=".$mem['id']);
				if(isset($registered[$mem['id']]))
				{
					$this->ipsclass->DB->do_insert('market_reward_logs',array(
																				'member_id' => $mem['id'],
																				'l_time' => time(),
																				'location' => 'registered',
																				'l_amount' => $this->ipsclass->vars['market_register'],
												)							);
				}
				if(isset($posted[$mem['id']]))
				{
					$this->ipsclass->DB->do_insert('market_reward_logs',array(
																				'member_id' => $mem['id'],
																				'l_time' => time(),
																				'location' => 'posts',
																				'l_amount' => $posted[$mem['id']],
												)							);
				}
			}
		}
		
		$this->class->unlock_task( $this->task );
	}
	
	function register_class(&$class)
	{
		$this->class     = &$class;
		$this->ipsclass  =& $class->ipsclass;
		$this->root_path = $this->class->root_path;
	}
	
	function pass_task( $this_task )
	{
		$this->task = $this_task;
	}
	
	
}
?>